package atm;

import atm.domain.Card;
import atm.domain.Cash;
import atm.domain.Receipt;
import atm.physical.CardReader;
import atm.physical.CashDispenser;
import atm.physical.ReceiptPrinter;
import atm.service.AccountService;
import atm.view.LoginController;
import atm.view.MenuController;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.Date;

public class Atm extends Application {

    private Stage primaryStage;

    private Card card;
    private AccountService accountService;
    private CardReader cardReader;
    private CashDispenser cashDispenser;
    private ReceiptPrinter receiptPrinter;


    public Atm() {
        this.accountService = new AccountService();
        this.cardReader = new CardReader();
        this.cashDispenser = new CashDispenser(500000000);
        this.receiptPrinter = new ReceiptPrinter();

    }

    public static void main(String[] args) {
        launch(args);
    }

	//插卡
	public boolean insertCard(String cardid){
		card = cardReader.readCard(cardid);
    	if(card != null)
    		return true;
//		session = new Session();
//		session.start();//开启session
//		display.showInfo("请输入银行卡密码");
    	else
		return false;
	}
	
    //验证密码
    public boolean validatePw(Card card, String password) {
        return accountService.validateAccount(card.getCardid(), password);

    }
    
    //取款
    public Boolean withdraw(Card card, int amount) {
        // TODO

        if (accountService.withdrawal(card.getCardid(), amount)) {
            cashDispenser.spitCash(amount);
            System.out.println("请取走您的现金");
            return true;
        } else {
            System.out.println("取款失败");
            return false;
        }
    }
    
    //吐钞
    public Cash spitCash(int amount) {
        return this.cashDispenser.spitCash(amount);
    }

    public String showInfo(int amount) {
        return String.valueOf(amount);
    }
    
    //打印凭证
    public Receipt print(Card card, int amount) {
        Date date = new Date();
        return this.receiptPrinter.print(card, date, amount);
    }

    //退卡
    public void exit() {
        cardReader.exitCard();
  
        //session.closeState = true;

    }

    public Card getCard() {
        return card;
    }


    public void setCard(Card card) {
        this.card = card;
    }

    public CardReader getCardReader() {
        return cardReader;
    }

    public ReceiptPrinter getReceiptPrinter() {
        return receiptPrinter;
    }

    //加载登录界面
    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        loadLoginView();
    }

    //登录界面
    public void loadLoginView() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(LoginController.class.getResource("Login.fxml"));
            Pane pane = loader.load();
            LoginController controller = loader.getController();

            controller.setAtm(this);
            Scene scene = new Scene(pane);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //取款界面
    public void loadMenu() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MenuController.class.getResource("menu.fxml"));
            Pane menu = loader.load();
            MenuController controller = loader.getController();
            controller.setAtm(this);
            Scene scene = new Scene(menu);
            primaryStage.setScene(scene);
            primaryStage.setTitle("ATM");
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
